package org.aman.generator.util;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Element;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @desc <p></p>
 * @author DaiJiangGuo
 * @date 2015年5月17日下午3:25:41
 */
public class AmanSaxWriter extends DefaultHandler {
    private Integer elementIndex = 0;
    List<Element> elementList = new ArrayList<Element>();

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        Element element = elementList.get(elementIndex);
        String order = "";
        for(int i=0;i<attributes.getLength();i++){
            order += ":"+attributes.getQName(i);
        }
        if(order.length()>0){
            order = order.substring(1);
        }
        element.setAttribute("aman_attr_order", order);
        elementIndex++;
    }

    @Override
    public void startDocument() throws SAXException {

    }

    @Override
    public void startPrefixMapping(String prefix, String uri) throws SAXException {
    }


    @Override
    public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException {
//        InputStream is = getClass().getClassLoader().getResourceAsStream("org/aman/generator/util/mybatis-3-mapper.dtd");
//        InputSource ins = new InputSource(is);
//        return ins;
        StringReader sr = new StringReader(""); //$NON-NLS-1$

        return new InputSource(sr);
    }

    public static void main(String[] args) {
        String s = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
                + "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >"+
                "<mapper namespace=\"com.flow.mapper.BannerMapper\" >\n"+
                "  <resultMap id=\"BaseResultMap\" type=\"com.flow.entity.Banner\" >\n"+
                "    <result column=\"title\" property=\"title\" jdbcType=\"DATE\" />\n"+
                "    <result column=\"test_date2\" property=\"testDate2\" jdbcType=\"DATE\" />\n"+
                "  </resultMap>\n"+
                "</mapper>\n";

        InputSource is = new InputSource(new StringReader(s));
        SAXParserFactory saxParFac = SAXParserFactory.newInstance();
        try {
            SAXParser saxParser = saxParFac.newSAXParser();
            saxParser.parse(is, new AmanSaxWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @author DaiJiangGuo
     * @data 2015年5月18日 上午11:32:43
     * @param targetFile
     * @param el
     */
    public void parse(File targetFile, List<Element> el) {
        SAXParserFactory saxParFac = SAXParserFactory.newInstance();
        try {
            elementList = el;
            SAXParser saxParser = saxParFac.newSAXParser();
            saxParser.parse(targetFile, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @author DaiJiangGuo
     * @data 2015年5月18日 上午11:43:46
     * @param is
     * @param el
     */
    public void parse(InputSource is, List<Element> el) {
        SAXParserFactory saxParFac = SAXParserFactory.newInstance();
        try {
            elementList = el;
            SAXParser saxParser = saxParFac.newSAXParser();
            saxParser.parse(is, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @author DaiJiangGuo
     * @data 2015年5月18日 上午11:45:05
     * @param s
     * @param el
     */
    public void parse(String s, List<Element> el) {
        InputSource is = new InputSource(new StringReader(s));
        SAXParserFactory saxParFac = SAXParserFactory.newInstance();
        try {
            elementList = el;
            SAXParser saxParser = saxParFac.newSAXParser();
            saxParser.parse(is, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
